<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>订单管理系统</title>
    <link rel="stylesheet" href="layui/css/layui.css">
    <link rel="stylesheet" href="css/my_css.css">
</head>

<body>
    <blockquote class="layui-elem-quote layui-text">
        查询库存
    </blockquote>
    <div class="larry-personal-body clearfix">
        <table class="layui-hide" id="stock_table" lay-filter="stock_table"></table>
    </div>
</body>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
</script>
<script type="text/javascript" src="layui/layui.js"></script>
<script type="text/javascript" src="js/jquery-3.5.1.min.js"></script>
<script type="text/javascript">
    layui.use('table', function() {
        let table = layui.table;
        table.render({
            elem: '#stock_table',
            url: 'get_all_products',
            page: false, //开启分页
            // toolbar: '#toolbarDemo',
            cols: [[
                { field: 'shopName', title: '商家名称', fixed: 'left', width: 160 },
                { field: 'productName', title: '商品名称', width: 160 },
                { field: 'productNum', title: '剩余数量', sort: true, width: 160 },
                { fixed: 'right', title:'操作', toolbar: '#barDemo', width: 80 }
            ]]
        });
        table.on('tool(stock_table)', function(obj) {
            let data = obj.data;
            if (obj.event === 'delete') {
                layer.confirm('确认删除该商品?', function(index) {
                    $.ajax({
                        url:'delete_product_by_name',
                        data: {
                            'productName': data.productName,
                            'shopName': data.shopName
                        },
                        method: 'post',
                        success: function (res) {
                            if (res) {
                                obj.del();
                                layer.msg('删除商品成功!', {
                                    icon: 1,
                                    time: 2000
                                });
                            }
                        },
                        error: function (msg) {
                            layer.msg('删除商品错误!', {
                                icon: 2,
                                time: 2000
                            });
                        }
                    });
                    layer.close(index);
                });
            }
        });
    });
</script>

</html>